<?php
/**
 * desc Created by wang.
 * User: wang
 * motto: 是时候为小时候吹过的牛而努力了!
 * Date: 2020/3/25 15:31
 */

namespace app\admin\business;


use app\common\lib\Arr;
use app\common\model\mysql\Manager as ManagerModel;
use think\facade\Config;

class Manager extends AdminBase
{
    /**
     * 功能说明 添加角色数据
     * @author: wang
     * @Date: 2020/3/25 15:54
     * @param $data
     * @return bool
     * @throws \think\Exception
     */
    public static function add($data)
    {
        $ManagerModel = new ManagerModel();
        $key = Config::load('key/auth_key', 'auth_key');
        $data['password'] = md5($key['admin_auth_key'] . md5($data['password']));
        try {
            $res = $ManagerModel->save($data);
        } catch (\Exception $e) {
            throw new \think\Exception('内部异常');
        }

        //记录日志
        self::insert_admin_log('添加了【' . $data['username'] . '】管理员信息');
        return $res;
    }

    /**
     * 功能说明 更新数据
     * @author: wang
     * @Date: 2020/3/25 18:24
     * @param $data
     * @return mixed
     * @throws \think\Exception
     */
    public static function edit($data)
    {
        $ManagerModel = new ManagerModel();
        if (empty($data['password'])) {
            unset($data['password']);
        } else {
            $key = Config::load('key/auth_key', 'auth_key');
            $data['password'] = md5($key['admin_auth_key'] . md5($data['password']));
        }
        if(!empty($data['icon'])){
            $data['icon'] = trim($data['icon'], '/');
        }
        try {
            $res = $ManagerModel->edit($data);
        } catch (\Exception $e) {
            throw new \think\Exception('内部异常');
        }

        //记录日志
        self::insert_admin_log('更新了【' . $data['username'] . '】管理员信息');
        return $res;
    }

    /**
     * 功能说明 获取角色列表
     * @author: wang
     * @Date: 2020/3/25 18:02
     * @param string $limit
     * @return array
     * @throws \think\db\exception\DbException
     */
    public static function getManagerList($where, $limit = '15')
    {
        $ManagerModel = new ManagerModel();
        $res = $ManagerModel->getManagerList($where, $limit)->toArray();
        foreach ($res['data'] as $k => $v) {
            if (!empty($v['icon'])) {
                $res['data'][$k]['icon'] = '/' . $v['icon'];
            } else {
                $res['data'][$k]['icon'] = '/static/admin/image/user_icon/head_default.gif';
            }
        }
        return $res;
    }

    /**
     * 功能说明 获取全部管理员信息
     * @author: wang
     * @Date: 2020/4/3 11:38
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getAllManager(){
        $ManagerModel = new ManagerModel();
        return $ManagerModel->getAllManager()->toArray();
    }

    /**
     * 功能说明 通过ID获取数据
     * @author: wang
     * @Date: 2020/3/25 20:50
     * @param $id
     * @return array|\think\Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public static function getDataById($id)
    {
        $ManagerModel = new ManagerModel();
        return $ManagerModel->getDataById($id);
    }

    /**
     * 功能说明 更新状态
     * @author: wang
     * @Date: 2020/3/25 17:56
     * @param $data
     * @return mixed
     */
    public static function upStatus($data)
    {
        $ManagerModel = new ManagerModel();
        //记录日志
        self::insert_admin_log('更新了ID为-【' . $data['id'] . '】的管理员状态');
        return $ManagerModel->upStatus($data);
    }

    /**
     * 功能说明 通过ID删除数据
     * @author: wang
     * @Date: 2020/3/25 20:54
     * @param $id
     * @return bool
     * @throws \think\Exception
     */
    public static function delDataById($id)
    {
        if ($id == 1) {
            throw new \think\Exception('无法删除超级管理员');
        }
        $ManagerModel = new ManagerModel();
        try {
            $res = $ManagerModel->delDataById($id);
        } catch (\Exception $e) {
            throw new \think\Exception('内部异常');
        }

        //记录日志
        self::insert_admin_log('删除了ID为-【' . $id . '】的管理员信息');
        return $res;
    }

    /**
     * 功能说明 通过ID批量删除数据
     * @author: wang
     * @Date: 2020/3/27 11:55
     * @param $ids
     * @return bool
     * @throws \think\Exception
     */
    public static function delDataByIds($ids)
    {
        if (empty($ids)) {
            throw new \think\Exception('未选择数据');
        }
        $idArr = explode(',', $ids);
        $key = array_search(1, $idArr);
        if ($key !== false) {
            array_splice($idArr, $key, 1);
        }
        $ManagerModel = new ManagerModel();
        try {
            $res = $ManagerModel->delDataById($idArr);
        } catch (\Exception $e) {
            throw new \think\Exception('内部异常');
        }

        //记录日志
        self::insert_admin_log('批量删除了ID为-【' . $ids . '】的角色信息');
        return $res;
    }

    /**
     * 功能说明 登录逻辑
     * @author: wang
     * @Date: 2020/3/31 15:10
     * @param $data
     * @return bool
     * @throws \think\Exception
     */
    public static function login($data)
    {

        $ManagerModel = new ManagerModel();
        $userData = self::getAdminUserByUserName($data['username']);
        if (empty($userData)) {
            throw new \think\Exception('用户不存在');
        }

        $key = Config::load('key/auth_key', 'auth_key');
        $password = md5($key['admin_auth_key'] . md5($data['password']));
        if ($userData['password'] != $password) {
            throw new \think\Exception('密码错误');
        }

        //更新用户信息
        $saveData = [
            'last_login_time' => time(),
            'last_login_ip' => request()->ip()
        ];
        try {
            $res = $ManagerModel->upateByID($userData['id'], $saveData);
        } catch (\Exception $e) {
            //todo 记录日志 $e->getMessage();
            throw new \think\Exception('内部异常,登录失败');
        }

        if (empty($res)) {
            throw new \think\Exception('登录失败');
        }


        //记录session
        session('session_admin', $userData);
        //记录日志
        self::insert_admin_log('【' . $userData['username'] . '】登录成功');
        return true;
    }

    /**
     * 功能说明 通过用户名获取用户信息
     * @author: wang
     * @Date: 2020/3/31 14:59
     * @param $username
     * @return bool
     */
    public static function getAdminUserByUserName($username)
    {
        $ManagerModel = new ManagerModel();
        $userData = $ManagerModel->getAdminUserByUserName($username);
        if (empty($userData) || $userData->status != 1) {
            return false;
        }
        $userData = $userData->toArray();
        return $userData;
    }


    /**
     * 功能说明 更新密码
     * @author: wang
     * @Date: 2020/3/31 17:50
     * @param $uid
     * @param $data
     * @return bool
     */
    public static function upPassword($adminUser, $data)
    {
        if($data['password'] == $data['password1']){
            throw new \think\Exception('新旧密码不能相同');
        }
        if($data['password1'] != $data['password2']){
            throw new \think\Exception('新密码不一致');
        }

        $key = Config::load('key/auth_key', 'auth_key');
        $password = md5($key['admin_auth_key'] . md5($data['password']));
        if($password != $adminUser['password']){
            throw new \think\Exception('旧密码错误');
        }

        $new_password = md5($key['admin_auth_key'] . md5($data['password1']));
        $ManagerModel = new ManagerModel();
        $saveData = [
            'password' => $new_password,
        ];
        try{
            $res = $ManagerModel->upateByID($adminUser['id'], $saveData);
        }catch (\Exception $e){
            throw new \think\Exception('内部错误');
        }
        session('session_admin.password', $new_password);
        //记录日志
        self::insert_admin_log('用户【' .$adminUser['username'] . '】密码修改成功');
        return $res;
    }
}